package cn.yunhe.crm.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yunhe.crm.service.BusinessService;
import cn.yunhe.crm.service.ContractService;
import cn.yunhe.crm.service.DiaryService;
import cn.yunhe.crm.service.OrganizationService;
import cn.yunhe.crm.service.TasklistService;

/**
 * 统计分析
 * @author Administrator
 *
 */
@Controller
public class TotalController {
	@Autowired
	@Qualifier("OrganizationService")
	private OrganizationService oService;
	
	public void setoService(OrganizationService oService) {
		this.oService = oService;
	}
	@Autowired
	@Qualifier("ContractService")
	private ContractService cService;
	
	public void setcService(ContractService cService) {
		this.cService = cService;
	}
	@Autowired
	@Qualifier("DiaryService")
	private DiaryService dService;
	
	public void setdService(DiaryService dService) {
		this.dService = dService;
	}
	@Autowired
	@Qualifier("TasklistService")
	private TasklistService tService;
	
	public void settService(TasklistService tService) {
		this.tService = tService;
	}
	@Autowired
	@Qualifier("BusinessService")
	private BusinessService bService;
	
	public void setbService(BusinessService bService) {
		this.bService = bService;
	}
	/**
	 * 查询机构、合同、日志、商机、任务总数
	 * @param request
	 * @return
	 */
	@RequestMapping("businessdata.do")
	public String checkCount(HttpServletRequest request){
		int ocount=oService.checkCount();
		request.removeAttribute("ocount");
		request.setAttribute("ocount", ocount);
		int ccount=cService.checkCount();
		request.removeAttribute("ccount");
		request.setAttribute("ccount", ccount);
		int dcount=dService.checkCount();
		request.removeAttribute("dcount");
		request.setAttribute("dcount", dcount);
		int tcount=tService.checkCount();
		request.removeAttribute("tcount");
		request.setAttribute("tcount", tcount);
		int bcount=bService.checkCount();
		request.removeAttribute("bcount");
		request.setAttribute("bcount", bcount);
		return "businessdata";
	}
	/**
	 * 根据日期查询机构、合同、日志、商机、任务总数
	 * @param request
	 * @return
	 */
	@RequestMapping("checkCount.do")
	public String checkCountByYearAndMonth(HttpServletRequest request){
			String begintime=request.getParameter("begintime");
			String endtime=request.getParameter("endtime");
			int ocount=oService.checkCountByYearAndMonth(begintime,endtime);
			request.removeAttribute("ocount");
			request.setAttribute("ocount", ocount);
			int ccount=cService.checkCountByYearAndMonth(begintime,endtime);
			request.removeAttribute("ccount");
			request.setAttribute("ccount", ccount);
			int dcount=dService.checkCountByYearAndMonth(begintime,endtime);
			request.removeAttribute("dcount");
			request.setAttribute("dcount", dcount);
			int tcount=tService.checkCountByYearAndMonth(begintime,endtime);
			request.removeAttribute("tcount");
			request.setAttribute("tcount", tcount);
			int bcount=bService.checkCountByYearAndMonth(begintime,endtime);
			request.removeAttribute("bcount");
			request.setAttribute("bcount", bcount);
			return "businessdata";
	}
	/**
	 * 查询产品A、B、C总数
	 * @param request
	 * @return
	 */
	@RequestMapping("productsale.do")
	public String checkProductCount(HttpServletRequest request){
		int aproduct=cService.checkProductA();
		request.removeAttribute("aproduct");
		request.setAttribute("aproduct", aproduct);
		int bproduct=cService.checkProductB();
		request.removeAttribute("bproduct");
		request.setAttribute("bproduct", bproduct);
		int cproduct=cService.checkProductC();
		request.removeAttribute("cproduct");
		request.setAttribute("cproduct", cproduct);
		return "productsale";
	}
	/**
	 * 根据时间查询产品A、B、C总数
	 * @param request
	 * @return
	 */
	@RequestMapping("checkProductCount.do")
	public String checkProductCountByYearAndMonth(HttpServletRequest request){
		String begintime=request.getParameter("begintime");
		String endtime=request.getParameter("endtime");
		int aproduct=cService.checkProductAbyYearAndMonth(begintime, endtime);
		request.removeAttribute("aproduct");
		request.setAttribute("aproduct", aproduct);
		int bproduct=cService.checkProductBbyYearAndMonth(begintime, endtime);
		request.removeAttribute("bproduct");
		request.setAttribute("bproduct", bproduct);
		int cproduct=cService.checkProductCbyYearAndMonth(begintime, endtime);
		request.removeAttribute("cproduct");
		request.setAttribute("cproduct", cproduct);
		return "productsale";
	}
	/**
	 * 查询不同阶段状态商机总数
	 * @param request
	 * @return+
	 */
	@RequestMapping("saleformpredict.do")
	public String checkCountByStage(HttpServletRequest request){
		int stageOne=bService.checkCountByStage(1);
		request.removeAttribute("stageOne");
		request.setAttribute("stageOne", stageOne);
		int stageTwo=bService.checkCountByStage(2);
		request.removeAttribute("stageTwo");
		request.setAttribute("stageTwo", stageTwo);
		int stageThree=bService.checkCountByStage(3);
		request.removeAttribute("stageThree");
		request.setAttribute("stageThree", stageThree);
		int stageFour=bService.checkCountByStage(4);
		request.removeAttribute("stageFour");
		request.setAttribute("stageFour", stageFour);
		int stageFive=bService.checkCountByStage(5);
		request.removeAttribute("stageFive");
		request.setAttribute("stageFive", stageFive);
		return "saleformpredict";
	}
	/**
	 * 根据时间查询不同阶段状态商机总数
	 * @param request
	 * @return
	 */
	@RequestMapping("checkCountByStageByYearAndMonth.do")
	public String checkCountByStageByYearAndMonth(HttpServletRequest request){
		String begintime=request.getParameter("begintime");
		String endtime=request.getParameter("endtime");
		int stageOne=bService.checkCountByStageByYearAndMonth(1, begintime, endtime);
		request.removeAttribute("stageOne");
		request.setAttribute("stageOne", stageOne);
		int stageTwo=bService.checkCountByStageByYearAndMonth(2, begintime, endtime);
		request.removeAttribute("stageTwo");
		request.setAttribute("stageTwo", stageTwo);
		int stageThree=bService.checkCountByStageByYearAndMonth(3, begintime, endtime);
		request.removeAttribute("stageThree");
		request.setAttribute("stageThree", stageThree);
		int stageFour=bService.checkCountByStageByYearAndMonth(4, begintime, endtime);
		request.removeAttribute("stageFour");
		request.setAttribute("stageFour", stageFour);
		int stageFive=bService.checkCountByStageByYearAndMonth(5, begintime, endtime);
		request.removeAttribute("stageFive");
		request.setAttribute("stageFive", stageFive);
		return "saleformpredict";
	}
	/**
	 * 查询赢单和合同总数和总金额
	 * @param request
	 * @return
	 */
	@RequestMapping("salegrade.do")
	public String checkSaleCount(HttpServletRequest request){
		//赢单总数量
		int ycount=bService.checkCountByStage(5);
		request.removeAttribute("ycount");
		request.setAttribute("ycount", ycount);
		//合同总数量
		int ccount=cService.checkCount();
		request.removeAttribute("ccount");
		request.setAttribute("ccount", ccount);
		//合同总金额
		int csum=cService.checkContractAmount();
		request.removeAttribute("csum");
		request.setAttribute("csum", csum);
		//赢单总金额
		int ysum=0;
		List<Integer> oids=bService.checkOidByStage();
		for (Integer oid : oids) {
			int amount=cService.checkAmountByOid(oid);
			ysum+=amount;
		}
		request.removeAttribute("ysum");
		request.setAttribute("ysum", ysum);
		return "salegrade";
	}
	/**
	 * 根据时间查询赢单和合同总数和总金额
	 * @param request
	 * @return
	 */
	@RequestMapping("checkSaleCount.do")
	public String checkSaleCountByYearAndMonth(HttpServletRequest request){
		//获取开始日期和结束日期
		String begintime=request.getParameter("begintime");
		String endtime=request.getParameter("endtime");
		//赢单总数量
		int ycount=bService.checkCountByStageByYearAndMonth(5, begintime, endtime);
		request.removeAttribute("ycount");
		request.setAttribute("ycount", ycount);
		//合同总数量
		int ccount=cService.checkContractAmountByYearAndMonth(begintime, endtime);
		request.removeAttribute("ccount");
		request.setAttribute("ccount", ccount);
		//合同总金额
		int csum=cService.checkContractAmountByYearAndMonth(begintime, endtime);
		request.removeAttribute("csum");
		request.setAttribute("csum", csum);
		//赢单总金额
		int ysum=0;
		List<Integer> oids=bService.checkOidByStageByYearAndMonth(begintime, endtime);
		for (Integer oid : oids) {
			int amount=cService.checkAmountByOid(oid);
			ysum+=amount;
		}
		request.removeAttribute("ysum");
		request.setAttribute("ysum", ysum);
		return "salegrade";
	}
}
